import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class XMLHandler {
	/*
	 * void fromXML(XMLStream) void addCommand(ServerCommand) serverCommand[]
	 * getCommand(); XMLStream toXML();
	 */
	private Vector<ServerCommand> commands;

	public XMLHandler() {

		this.commands = new Vector<ServerCommand>();

	}
	
	public boolean isEmpty() {
		if(this.commands.isEmpty())
			return true;
		else
			return false;
	}
	

	public Integer getValue(Integer i, String valueName) {
		return commands.elementAt(i).getValueOfParameter(valueName);
	}

	public void addCommand(ServerCommand cmd) {
		commands.addElement(cmd);
	}

	public Vector<ServerCommand> getCommand() {
		return commands;
	}

	public String getName(Integer i) {
		return commands.elementAt(i).getName();
	}

	public Boolean isMove(Integer i) {
		return commands.elementAt(i).isMove();
	}

	public Boolean isReboot(Integer i) {
		return commands.elementAt(i).isReboot();
	}

	public Boolean isRotate(Integer i) {
		return commands.elementAt(i).isRotate();
	}
	
	public Boolean isClose(Integer i) {
		return commands.elementAt(i).isClose();
	}

	public Boolean isScan(Integer i) {
		return commands.elementAt(i).isScan();
	}
	
	public Boolean isAuto(Integer i) {
		return commands.elementAt(i).isAutoMode();
	}

	public void fromXML(String xmlString) throws XMLStreamException {

		if (!xmlString.isEmpty()) {

			XMLStreamReader StreamReader;
			XMLInputFactory InputFactory = XMLInputFactory.newInstance();

			InputStream is = new ByteArrayInputStream(xmlString.getBytes());
			StreamReader = InputFactory.createXMLStreamReader(is);

			String localName = "";
			String value = "";
			Integer next = 0;

			Vector<CommandParameter> tempParam = new Vector<CommandParameter>();

			while (StreamReader.hasNext()) {
				next = StreamReader.next();
				if (next == XMLStreamReader.START_ELEMENT) {
					localName = StreamReader.getLocalName();
					if(!localName.equals("s2r")) {
					try {
						for (Integer i = 0; i < StreamReader
								.getAttributeCount(); i++) {

							tempParam
									.addElement(new CommandParameter(
											StreamReader.getAttributeName(i)
													.toString(),
											Integer.parseInt(StreamReader
													.getAttributeValue(i))));
						}
						addCommand(new ServerCommand(localName, tempParam));
					} catch (Exception e) {

					}
				}
				}
				tempParam.clear();
			}
		}
	}

	public String toString(String root) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		sb.append("<" + root + ">\n");
		for (int i = 0; i < commands.size(); i++) {
			sb.append("<" + commands.elementAt(i).getName());
			for (Integer j = 0; j < commands.elementAt(i).commandParameterCount(); j++) {
				sb.append(" "
						+ commands.elementAt(i).getCommandParameter(j)
								.getName()
						+ "=\""
						+ commands.elementAt(i).getCommandParameter(j)
								.getValue().toString() + "\"");
			}
			sb.append("/>\n");
		}

		sb.append("</" + root + ">");
		return sb.toString();
	}

	public void clear() {
		commands.clear();
	}

}
